package cn.lifeaid.shiro;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 自定义MyFormAuthenticationFilter，继承FormAuthenticationFilter，方便在登录成功和登录失败后做一些特殊操作
 * 
 * @author shi_senlin
 *
 */
public class MyFormAuthenticationFilter extends FormAuthenticationFilter {

	@Autowired
	InitSystemData initSystemData;

	/**
	 * 登录成功后调用的该方法
	 */
	@Override
	protected boolean onLoginSuccess(AuthenticationToken token,
			Subject subject, ServletRequest request, ServletResponse response)
			throws Exception {
		// 在用户登录成功后，加载右侧菜单栏
		Subject currentUser = SecurityUtils.getSubject();
		initSystemData.initMenu(currentUser);
		return super.onLoginSuccess(token, subject, request, response);
	}

	/**
	 * 登录失败后调用的该方法(暂时不需扩展)
	 */
	@Override
	protected boolean onLoginFailure(AuthenticationToken token,
			AuthenticationException e, ServletRequest request,
			ServletResponse response) {
		return super.onLoginFailure(token, e, request, response);
	}

	@Override
	protected boolean isAccessAllowed(ServletRequest request,
			ServletResponse response, Object mappedValue) {
		//System.out.println("xx");
		// TODO Auto-generated method stub
		return super.isAccessAllowed(request, response, mappedValue);
	}

}
